<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>冷库温湿度监控</title>
    <!-- 使用CDN引入CSS -->
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/datatables/1.10.21/css/jquery.dataTables.min.css">
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/datatables.net-buttons-bs4/2.2.3/buttons.bootstrap4.min.css">
    <style>
        .monitoring-card {
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .monitoring-card .card-header {
            border-radius: 10px 10px 0 0;
            font-weight: bold;
        }
        .monitoring-card .card-body {
            padding: 20px;
        }
        .monitoring-count {
            font-size: 2rem;
            font-weight: bold;
        }
        .trend-chart {
            height: 300px;
            margin-bottom: 20px;
        }
        .alarm-badge {
            padding: 5px 10px;
            border-radius: 15px;
            font-weight: bold;
        }
        .alarm-badge.normal {
            background-color: #28a745;
            color: white;
        }
        .alarm-badge.warning {
            background-color: #ffc107;
            color: black;
        }
        .alarm-badge.danger {
            background-color: #dc3545;
            color: white;
        }
        .progress {
            height: 10px;
            margin-top: 5px;
        }
        .progress-bar-normal {
            background-color: #28a745;
        }
        .progress-bar-warning {
            background-color: #ffc107;
        }
        .progress-bar-danger {
            background-color: #dc3545;
        }
        .real-time-container {
            max-height: 400px;
            overflow-y: auto;
        }
        .refresh-btn {
            margin-left: 10px;
        }
        .warehouse-select-container {
            margin-bottom: 20px;
        }
        .status-unprocessed {
            color: #dc3545;
            font-weight: bold;
        }
        .status-processed {
            color: #28a745;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <h2 class="mt-4 mb-4">冷库温湿度监控</h2>
        
        <!-- 仓库选择和刷新按钮 -->
        <div class="row warehouse-select-container">
            <div class="col-md-4">
                <div class="input-group">
                    <div class="input-group-prepend">
                        <label class="input-group-text" for="warehouseSelect">选择仓库</label>
                    </div>
                    <select class="custom-select" id="warehouseSelect">
                        <option value="all" selected>所有仓库</option>
                        <!-- 这里将通过AJAX加载仓库选项 -->
                                </select>
                    <div class="input-group-append">
                        <button class="btn btn-primary refresh-btn" id="refreshBtn">
                            <i class="fas fa-sync-alt"></i> 刷新数据
                                </button>
                    </div>
                </div>
                            </div>
                        </div>
                        
        <!-- 监控数据概览 -->
        <div class="row">
                            <div class="col-md-3">
                <div class="card monitoring-card bg-light">
                    <div class="card-header bg-primary text-white">
                        <i class="fas fa-thermometer-half mr-2"></i>监控点总数
                    </div>
                                    <div class="card-body text-center">
                        <div class="monitoring-count" id="totalCount">0</div>
                        <div class="text-muted">实时监控点</div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-3">
                <div class="card monitoring-card bg-light">
                    <div class="card-header bg-success text-white">
                        <i class="fas fa-check-circle mr-2"></i>正常监控点
                    </div>
                                    <div class="card-body text-center">
                        <div class="monitoring-count text-success" id="normalCount">0</div>
                                        <div class="progress">
                            <div class="progress-bar progress-bar-normal" id="normalProgress" role="progressbar" style="width: 0%"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-3">
                <div class="card monitoring-card bg-light">
                    <div class="card-header bg-warning text-dark">
                        <i class="fas fa-exclamation-triangle mr-2"></i>预警监控点
                    </div>
                                    <div class="card-body text-center">
                        <div class="monitoring-count text-warning" id="warningCount">0</div>
                                        <div class="progress">
                            <div class="progress-bar progress-bar-warning" id="warningProgress" role="progressbar" style="width: 0%"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-3">
                <div class="card monitoring-card bg-light">
                    <div class="card-header bg-danger text-white">
                        <i class="fas fa-radiation mr-2"></i>告警监控点
                    </div>
                                    <div class="card-body text-center">
                        <div class="monitoring-count text-danger" id="dangerCount">0</div>
                                        <div class="progress">
                            <div class="progress-bar progress-bar-danger" id="dangerProgress" role="progressbar" style="width: 0%"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <!-- 实时监控数据 -->
        <div class="card mb-4">
                            <div class="card-header">
                <i class="fas fa-table mr-1"></i>
                实时监控数据
                            </div>
                            <div class="card-body">
                <div class="real-time-container" id="realTimeData">
                    <!-- 这里将通过JavaScript动态生成监控数据卡片 -->
                                </div>
                            </div>
                        </div>
                        
        <!-- 趋势图表 -->
        <div class="row">
            <div class="col-md-6">
                <div class="card mb-4">
                            <div class="card-header">
                        <i class="fas fa-chart-line mr-1"></i>
                        温度趋势
                            </div>
                            <div class="card-body">
                        <div id="temperatureChart" class="trend-chart"></div>
                    </div>
                </div>
                                    </div>
                                    <div class="col-md-6">
                <div class="card mb-4">
                    <div class="card-header">
                        <i class="fas fa-chart-line mr-1"></i>
                        湿度趋势
                    </div>
                    <div class="card-body">
                        <div id="humidityChart" class="trend-chart"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
        <!-- 告警记录 -->
        <div class="card mb-4">
                            <div class="card-header">
                <i class="fas fa-bell mr-1"></i>
                告警记录
                            </div>
                            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered" id="alarmTable" width="100%" cellspacing="0">
                                    <thead>
                                        <tr>
                                <th>监控点ID</th>
                                            <th>仓库名称</th>
                                            <th>存储区域</th>
                                            <th>设备ID</th>
                                            <th>温度</th>
                                            <th>湿度</th>
                                            <th>告警信息</th>
                                            <th>记录时间</th>
                                            <th>处理状态</th>
                                            <th>操作</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                            <!-- 这里将通过AJAX加载告警记录数据 -->
                                    </tbody>
                                </table>
                </div>
            </div>
        </div>
    </div>
    
    <!-- 处理告警的模态框 -->
    <div class="modal fade" id="processAlarmModal" tabindex="-1" role="dialog" aria-labelledby="processAlarmModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="processAlarmModalLabel">处理告警</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="processAlarmForm">
                        <input type="hidden" id="alarmId" name="alarmId">
                        <div class="form-group">
                            <label for="alarmInfo">告警信息</label>
                            <input type="text" class="form-control" id="alarmInfo" readonly>
                        </div>
                        <div class="form-group">
                            <label for="processRemarks">处理备注</label>
                            <textarea class="form-control" id="processRemarks" name="processRemarks" rows="3" required></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="saveProcessBtn">保存处理</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 使用CDN引入JS -->
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
        // 检查jQuery是否正确加载
        console.log('jQuery已加载，版本:', $.fn.jquery);
    </script>
    <script src="https://cdn.bootcdn.net/ajax/libs/popper.js/1.16.1/umd/popper.min.js"></script>
    <script>
        // 检查popper.js是否正确加载
        console.log('Popper.js已加载:', typeof Popper !== 'undefined');
    </script>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js"></script>
    <script>
        // 检查Bootstrap是否正确加载
        console.log('Bootstrap已加载，modal函数可用:', typeof $.fn.modal === 'function');
    </script>
    <!-- 其他JS库 -->
    <script src="https://cdn.bootcdn.net/ajax/libs/chart.js/3.7.0/chart.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
    <script th:src="@{/js/warehouse/coldStorageMonitor.js}"></script>
    <script>
        $(document).ready(function() {
            // 检查Bootstrap modal函数是否可用
            console.log('jQuery版本:', $.fn.jquery);
            console.log('Bootstrap modal是否可用:', (typeof $.fn.modal === 'function'));
            
            // 移除可能存在的DataTables脚本标签
            $('script[src*="datatables"]').remove();
            
            // 动态加载DataTables库
            function loadDataTables() {
                return new Promise((resolve, reject) => {
                    // 尝试从CDN加载
                    $.getScript('https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js')
                        .done(function() {
                            console.log('DataTables core loaded from CDN');
                            resolve();
                        })
                        .fail(function() {
                            console.log('Failed to load DataTables from CDN, trying local backup');
                            // 从本地加载备份
                            $.getScript('/js/jquery.dataTables.min.js')
                                .done(function() {
                                    console.log('DataTables core loaded from local backup');
                                    resolve();
                                })
                                .fail(function() {
                                    console.error('Failed to load DataTables from local backup');
                                    reject();
                                });
                        });
                });
            }
            
            function loadDataTablesBS4() {
                return new Promise((resolve, reject) => {
                    // 尝试从CDN加载
                    $.getScript('https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap4.min.js')
                        .done(function() {
                            console.log('DataTables BS4 loaded from CDN');
                            resolve();
                        })
                        .fail(function() {
                            console.log('Failed to load DataTables BS4 from CDN, trying local backup');
                            // 从本地加载备份
                            $.getScript('/js/dataTables.bootstrap4.min.js')
                                .done(function() {
                                    console.log('DataTables BS4 loaded from local backup');
                                    resolve();
                                })
                                .fail(function() {
                                    console.error('Failed to load DataTables BS4 from local backup');
                                    reject();
                                });
                        });
                });
            }
            
            function loadDataTablesButtons() {
                return new Promise((resolve, reject) => {
                    // 尝试从CDN加载
                    $.getScript('https://cdn.bootcdn.net/ajax/libs/datatables.net-buttons/2.2.3/js/dataTables.buttons.min.js')
                        .done(function() {
                            console.log('DataTables Buttons loaded from CDN');
                            resolve();
                        })
                        .fail(function() {
                            console.log('Failed to load DataTables Buttons from CDN, trying local backup');
                            // 从本地加载备份
                            $.getScript('/js/dataTables.buttons.min.js')
                                .done(function() {
                                    console.log('DataTables Buttons loaded from local backup');
                                    resolve();
                                })
                                .fail(function() {
                                    console.error('Failed to load DataTables Buttons from local backup');
                                    reject();
                                });
                        });
                });
            }
            
            function loadDataTablesButtonsHTML5() {
                return new Promise((resolve, reject) => {
                    // 尝试从CDN加载
                    $.getScript('https://cdn.bootcdn.net/ajax/libs/datatables.net-buttons/2.2.3/js/buttons.html5.min.js')
                        .done(function() {
                            console.log('DataTables Buttons HTML5 loaded from CDN');
                            resolve();
                        })
                        .fail(function() {
                            console.log('Failed to load DataTables Buttons HTML5 from CDN, trying local backup');
                            // 从本地加载备份
                            $.getScript('/js/buttons.html5.min.js')
                                .done(function() {
                                    console.log('DataTables Buttons HTML5 loaded from local backup');
                                    resolve();
                                })
                                .fail(function() {
                                    console.error('Failed to load DataTables Buttons HTML5 from local backup');
                                    reject();
                                });
                        });
                });
            }
            
            // 加载所有DataTables相关库
            loadDataTables()
                .then(() => loadDataTablesBS4())
                .then(() => loadDataTablesButtons())
                .then(() => loadDataTablesButtonsHTML5())
                .then(() => {
                    console.log('All DataTables scripts loaded');
                    initDataTable();
                })
                .catch(() => {
                    console.error('Failed to load DataTables scripts');
                    alert('加载DataTables库失败，请刷新页面重试');
                });
            
            function initDataTable() {
                // 检查DataTable是否已成功加载
                if ($.fn.dataTable && $.fn.dataTable.defaults) {
                    console.log('DataTable is available, initializing...');
                    // 初始化DataTable
                    $('#monitorTable').DataTable({
                        "responsive": true,
                        "language": {
                            "url": '/js/datatables-chinese.json'
                        },
                        "lengthChange": true,
                        "autoWidth": false,
                        "searching": true,
                        "paging": true,
                        "info": true,
                        "buttons": ['copy', 'excel']
                    });
                } else {
                    console.error('DataTable is not available');
                    setTimeout(function() {
                        console.log('Retrying DataTable initialization...');
                        initDataTable();
                    }, 1000);
                }
            }
            
            // 其他原有的初始化代码...
        });
    </script>
</body>
</html> 